import * as API from '../../api/contactsApi';
import * as util from '../../utils/util';

let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    placeholder: '输入客户名称/手机号搜索',
    searchKey: '',
    pageNo: 1,
    pageSize: 10,
    total: 0,
    hasRequest: false,
    networkError: false,
    currentIndex: '', // 当前选中的index
    dataSource: [],
    hasMore: true,
    isAdmin: false
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 重新加载
    this.clear();
    this.loadData(false);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    // this.setData({
    //   isAdmin: app.globalData.user.isAdmin
    // })
    wx.getStorage({
      key: 'choosedClient',
      success: function (res) {
        if (res.data && res.data.id) {
          that.setData({
            currentIndex: res.data.id
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.clear();
    this.loadData(true);
  },

  clear: function () {
    this.setData({
      dataSource: [],
      hasMore: true,
      pageNo: 1,
      pageSize: 10,
    })
  },

  //实时搜索
  onHandleSearch(e) {
    this.setData({
      searchKey: e.detail + ""
    });
    this.goToSearch();
  },

  handleSearch(e) {
    if (!e.detail) {
      wx.showToast({
        icon: 'none',
        title: '请输入搜索关键字',
      })
      return;
    } else {
      this.setData({
        searchKey: e.detail + ""
      });
      this.goToSearch();
    }
  },

  clearSearch: function () {
    this.setData({
      searchKey: '',
      pageNo: 1,
      pageSize: 10,
      hasMore: true,
      dataSource: [],
    });
    this.loadData(true);
  },

  loadData: function (options) {
    if (!this.data.hasMore) return;
    API.contactsList(this.data).then(res => {
      let arr = res.customAccountList;
      //分页false
      if (!options) {
        arr = this.data.dataSource.concat(res.customAccountList || []);
      }
      let hasMore = arr.length < res.totalCount;
      this.setData({
        dataSource: arr,
        hasMore: hasMore,
        hasRequest: true,
        total: res.totalCount
      });
    }, err => {

    });
  },

  onReachBottom: function () {
    // 增量加载
    if (this.data.hasMore) {
      this.setData({
        pageNo: ++this.data.pageNo
      });
      this.loadData(false);
    }
  },

  //前往详情页面
  gotoDetail: function (e) {
    util.preventMoreClick(this, () => {
      const id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: './contactsDetail/contactsDetail?id=' + id
      });
    })
  },

  //前往新增页面

  gotoAdd: function (e) {
    util.toast('体验环境不可用');
  },

  gotoContactsSearch: function () {
    wx.navigateTo({
      url: '../contacts/contactsSearch/contactsSearch',
    })
  },

  gotoOrderList: function (options) {
    util.preventMoreClick(this, () => {
      var id = options.id;
      var customId = this.data.customId;
      wx.navigateTo({
        url: '../contactsOrderList/contactsOrderList?contactsId=' + id + '&customId=' + customId,
      })
    })
  },

  goToSearch: function () {
    var postbody = {
      pageNo: this.data.pageNo,
      pageSize: 20,
      searchKey: this.data.searchKey
    };
    API.contactsList(postbody).then(res => {
      let dataSource = res.customAccountList;
      this.setData({
        dataSource: dataSource,
        hasRequest: true
      });
    }, err => {
      this.setData({
        networkError: true
      });
      wx.showToast({
        title: err || '失败,请稍候再试',
        icon: 'none'
      });
    });
  },

  checkCell(e) {
    util.preventMoreClick(this, () => {
      let list = e.currentTarget.dataset.list.id;
      this.setData({
        currentIndex: list
      });
      wx.setStorage({
        key: 'choosedClient',
        data: e.currentTarget.dataset.list
      })
      const url = '../../../orderShare/orderShare'
      wx.navigateTo({
        url: url
      });
    })
  }
})